var tyfzhsmc = {
    //当前打开的行
    expandIndex: -1,
    pagination: true,
    rownumbers: true,
    //新增状态
    formStatus: false,
    //判断按钮显示
    setBtnStatus: function () {
        var row = $('#tyfzhsmc_grid').datagrid('getSelected');
        if (row && row.id) {
            $("#tyfzhsmc_del").show();
        } else {
            $("#tyfzhsmc_del").hide();
        }
    },
    //重置状态
    resetStatus: function () {
        tyfzhsmc.setBtnStatus();
        tyfzhsmc.expandIndex = -1;
        tyfzhsmc.formStatus = false;
    }
}

$(function () {
    //表格初始加载
    $('#tyfzhsmc_grid').datagrid({
        url: $.webPath() + "/cwgl/kmys/tyfzhsmc/tyfzhsmcList",
        toolbar: "#tyfzhsmc_toolbar",
        fitColumns: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'mc', title: '名称', width: 25,align: 'center'},
                {field: 'code', title: '代码', width: 25,align: 'center'},
                {field: 'dl', title: '类型', width: 15,align: 'center',
                    formatter: function (value) {
                        if (value == 'fzhs')
                            return '辅助核算';
                        if(value=='xjllxm')
                           return '现金流量项目';
                        else
                            return '项目';
                    }}
         ]],
        view: detailview,
        detailFormatter: function () {
            return '<div class="detail"></div>';
        },
        onExpandRow: function (index, row) {
            $("#layout_tyfzhsmc").mask();
            //关闭展开的行
            if (tyfzhsmc.expandIndex != -1) {
                $('#tyfzhsmc_grid').datagrid("collapseRow", tyfzhsmc.expandIndex);
            }
            //记录当前展开哪行
            tyfzhsmc.expandIndex = index;
            show_tyfzhsmc_form(index, row);//加载表格面板
        },
        onCollapseRow: function () {
            //删除当前记录的行
            tyfzhsmc.expandIndex = -1;
        },
        onLoadSuccess: tyfzhsmc.setBtnStatus,
        onClickRow: tyfzhsmc.setBtnStatus,
        onSelect: tyfzhsmc.setBtnStatus,
        onDblClickCell: function (index, field) {
            //双击事件
            if (field == "mc") {
                $('#tyfzhsmc_grid').datagrid("expandRow", index);
            }
        }
    });

//*************按钮触发事件***********
//新增触发
    $('#tyfzhsmc_add').click(
        tyfzhsmc_newItem
    );
//删除触发
    $('#tyfzhsmc_del').click(
        tyfzhsmc_destroyItem
    );
//搜索触发
    $('#tyfzhsmc_search').click(
        tyfzhsmc_search
    );
//*************按钮触发事件***********
});

//新增通用辅助核算
function tyfzhsmc_newItem() {
    //关闭打开的行
    if (tyfzhsmc.expandIndex != -1) {
        $('#tyfzhsmc_grid').datagrid("collapseRow", tyfzhsmc.expandIndex);
    }

    //已有新增判断
    if (!tyfzhsmc.formStatus) {
        $('#tyfzhsmc_grid').datagrid('insertRow', {index: 0, row: {isNewRecord: true}});
        tyfzhsmc.formStatus = true;
    }
    $('#tyfzhsmc_grid').datagrid('expandRow', 0);
//    $('#tyfzhsmc_grid').datagrid('getRowDetail', 0).find("[name='tyfzhsmc.qxList.id']").qxWin();

    $('#tyfzhsmc_grid').datagrid('selectRow', 0);
}

// 保存通用辅助核算新增表单
function tyfzhsmc_saveItem(index) {
    $("#layout_tyfzhsmc").mask();
    $('#tyfzhsmc_grid').datagrid('getRowDetail', index).find('form').form('submit', {
        url: $.webPath() + '/cwgl/kmys/tyfzhsmc/add_tyfzhsmc',
        onSubmit: function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                $("#layout_tyfzhsmc").mask('hide');
                return false;
            }
            return isValid;
        },
        success: function (data) {
            $("#layout_tyfzhsmc").mask("hide");
            var obj = eval('(' + data + ')');
            if (obj.statusCode == 200) {
                $('#tyfzhsmc_grid').datagrid('reload');
                tyfzhsmc.resetStatus();
                $.messager.show({
                    title: '提示',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else
                $.messager.alert('提示', obj.message, 'info');
        }
    });

}

//取消通用辅助核算表单
function tyfzhsmc_cancelItem(index) {
    var row = $('#tyfzhsmc_grid').datagrid('getRows')[index];
    if (row.isNewRecord) {
        $('#tyfzhsmc_grid').datagrid('deleteRow', index);
    } else {
        $('#tyfzhsmc_grid').datagrid('collapseRow', index);
    }
    $('#tyfzhsmc_grid').datagrid('reload');
    tyfzhsmc.resetStatus();
}
//删除通用辅助核算
function tyfzhsmc_destroyItem() {
    var row = $('#tyfzhsmc_grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该通用辅助核算', function (r) {
            if (r) {
                var index = $('#tyfzhsmc_grid').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/kmys/tyfzhsmc/delete_tyfzhsmc",
                    dataType: 'json',
                    type: 'POST',
                    data: {tyfzhsmcId: row.id},
                    beforeSend: function () {
                        $("#layout_tyfzhsmc").mask();
                    },
                    success: function (data) {
                        $("#layout_tyfzhsmc").mask("hide");
                        if (data.statusCode == 200) {
                            $('#tyfzhsmc_grid').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            tyfzhsmc.resetStatus();
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    }
                });
            }
        });
    } else {
        alert("请选择要删除的通用辅助核算");
    }
}


//查询通用辅助核算
function tyfzhsmc_search() {
    $('#tyfzhsmc_grid').datagrid('load', {
        mc: $('#tyfzhsmc_mc').val()
    });
}

//展开通用辅助核算表单
function show_tyfzhsmc_form(index, row) {
    $("#layout_tyfzhsmc").mask("hide");
    var ddv = $("#tyfzhsmc_grid").datagrid('getRowDetail', index).find('div.detail');
    var url = $.webPath() + '/cwgl/kmys/tyfzhsmc/tyfzhsmc_content?index=' + index;
    if (row.id)
        url += '&tyfzhsmcId=' + row.id;
    ddv.panel({
        border: false,
        cache: true,
        href: url,
        onLoad: function () {
            $('#tyfzhsmc_grid').datagrid('fixDetailRowHeight', index);
            $('#tyfzhsmc_grid').datagrid('selectRow', index);
            tyfzhsmc.setBtnStatus();
        }
    });
    $('#tyfzhsmc_grid').datagrid('fixDetailRowHeight', index);
}